﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CodeFarmer.Model.Entities;
using CodeFarmer.DataAccess.Infrastructure;

namespace CodeFarmer.DataAccess
{
    public interface IUserRepository : IRepository<User>
    {
        User GetUser(string userName);
        Boolean ValidateUser(string userName, string passWord);
        Boolean ExistEmail(string email);
        Boolean ExistUserName(string userName);
        Boolean ExistNickName(string nickName);
    }

    public class UserRepository : BaseRepository<User>, IUserRepository
    {
        public UserRepository(IDbFactory dbFactory)
            : base(dbFactory)
        {
        }

        public User GetUser(string userName)
        {
            return this.DB.User.FirstOrDefault(t => t.UserName == userName);
        }

        public Boolean ValidateUser(string userName, string passWord)
        {
            var query = this.DB.User.Where(t => t.UserName == userName && t.PassWord == passWord);
            if (query.Count() > 0)
                return true;
            else
                return false;
        }

        public Boolean ExistEmail(string email)
        {
            var query = this.DB.User.Where(t => t.Email == email);
            if (query.Count() > 0)
                return true;
            else
                return false;
        }

        public Boolean ExistUserName(string userName)
        {
            var query = this.DB.User.Where(t => t.UserName == userName);
            if (query.Count() > 0)
                return true;
            else
                return false;
        }

        public Boolean ExistNickName(string nickName)
        {
            var query = this.DB.User.Where(t => t.NickName == nickName);
            if (query.Count() > 0)
                return true;
            else
                return false;
        }
    }
}
